import { storeBindingsBehavior } from 'mobx-miniprogram-bindings'
import base from '@src/store/base'
import member from '@src/store/member'
import { getCategoryAvailable, getMemberMineInfo } from '@src/api/member'
import router from '@src/router'
import {
  parseCategoryIntro,
  parseCategoryRights,
  parsePrivileges,
  parseRechargeAwardRule,
  parseOpenCardGift
} from "@src/common/memberCardUtils";
Component({
  /**
   * 组件的属性列表
   */
  behaviors: [storeBindingsBehavior, router.routerBehavior],
  storeBindings: [
    {
      store: base,
      fields: ['themeColor', 'themeTitleColor', 'isiPhoneX']
    },
    {
      store: member,
      fields: ['isMember', 'memberName', 'categoryName', 'cardOrder']
    }
  ],
  properties: {
    modules: {
      type: Array,
      value: "",
    },
    login: {
      type: Boolean
    }
  },
  options: {
    multipleSlots: true,
    addGlobalClass: true
  },
  observers: {
    'isMember': function (isMember) {
      this.updateStoreBindings()
    },
    'modules': function () {
      this.init()
    }
  },
  /**
   * 组件的初始数据
   */
  data: {
    featureList: [],
    activeId: null,
    openCardGift: [],
    customOpenRewards: [],
    modulesData: null,
    enableCardFields: false,
    mineMsg: null,
    cardFields: [],
    pic: null,
  },
  lifetimes: {
    attached: function () {
      this.init()
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    init() {
      this.updateStoreBindings()
      let modulesData = this.data.modules[0];
      this.setData({ modulesData });
      let params = null;
      console.log('modulesData', modulesData);
      this.setData({ pic: modulesData.pic })
      if (modulesData.biz) {
        params = JSON.parse(modulesData.biz.params);
        this.setData({ enableCardFields: params.enableCardFields, cardFields: params.cardFields })
      }
      this.initData(params);
      if (this.data.isMember) { this.getMemberMineInfoFunc() }
    },
    initData(params) {
      console.log('params', params)
      getCategoryAvailable().then(res => {
        let val = res.data[0];
        this.setData({ featureList: res.data });
        this.findUpdateCard(res.data)
        if (params.defaultCardId) {
          res.data.forEach(item => {
            if (item.category.id == params.defaultCardId) {
              this.resetData(item);
            }
          })
        } else {
          this.resetData(val);
        }
        // let customOpenRewards = [];
        // if (item.category.customGift) { customOpenRewards = JSON.parse(item.category.customGift) }
        // let openCardGift = parseOpenCardGift(item.category, item.openCardCouponAward, true);
        // this.setData({ featureList: res.data, activeId: item.category.id, openCardGift, customOpenRewards });
      })
    },
    getIndexList(e) {
      let item = e.currentTarget.dataset.item;
      let id = item.category.id;
      if (id == this.data.activeId) { return; };
      this.resetData(item);
    },
    resetData(item) {
      let openCardGift = parseOpenCardGift(item.category, item.openCardCouponAward, true);
      let customOpenRewards = [];
      if (item.category.customGift) {
        customOpenRewards = JSON.parse(item.category.customGift)
      }
      this.setData({ openCardGift, customOpenRewards, activeId: item.category.id })
    },
    findUpdateCard(availableData) {
      console.log('this.data.cardOrder', this.data.cardOrder)
      for (let card of availableData) {
        if (card.category.cardOrder > this.data.cardOrder) {
          let category = card.category
          let rechargeAward = card.rechargeAward
          let categoryRights = parseCategoryRights(category, rechargeAward).concat(parseRechargeAwardRule(rechargeAward))
          let validPeriod = ""
          if (category.validPeriod === 0) {
            validPeriod = '永久'
          } else {
            validPeriod = `${category.validPeriod}年`
          }
          this.setData({ category, categoryRights, validPeriod })
          return;
        }
        this.setData({ category: null })
      }
    },
    getMemberMineInfoFunc() {
      getMemberMineInfo().then(res => {
        if (res.code == 0) { this.setData({ mineMsg: res.data }) }
      })
    },
    onLoad() {
    },
    openCard() {
      // if (!this.data.login) { this.triggerEvent('login'); return; }
      let urlParams = { categoryId: this.data.activeId }
      router.push({ name: 'indexChooseCard', params: urlParams })
    },
    openUpdateCard() {
      // if (!this.data.login) { this.triggerEvent('login'); return; }
      let urlParams = { categoryId: this.data.category.id }
      router.push({ name: 'indexChooseCard', params: urlParams })
    },
  }
})
